package com.liudehua.boot.context;

import java.util.HashMap;
import java.util.Map;

/**
 * 该类的参数用于透传给下游的服务
 * 自定义Context
 * ThreadLocal可以理解为Map<Thread,Map<CONTEXT,Context>>
 */
public class Context {
    //单例
    private static final ThreadLocal<Context> CONTEXT = new ThreadLocal<Context>(){
        @Override
        protected Context initialValue() {
            return new Context();
        }
    };


    private final static Context INSTANCE = new Context();
    private Context(){

    }

    public static Context getInstance(){
        return INSTANCE;
    }


    private final Map<String,Object> parameters = new HashMap<>();

    public void put(String str,Object obj){
        CONTEXT.get().parameters.put(str,obj);
    }

    public  Object get(String str){
       return  CONTEXT.get().parameters.get(str);
    }

    public static void remove(){
        CONTEXT.remove();
    }

}
